package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Calendar;

import dao.DatabaseOperations;
import models.CourseAboutBean;
import models.CourseBean;
import models.CourseCommentBean;
import models.CoursePrerequisiteBean;

/**
 * Servlet implementation class CourseServlet
 */
@WebServlet("/Course")
public class Course extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Course() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try{
			System.out.println("In the Course Servlet");
			
			CourseBean cb=new CourseBean();
			CourseAboutBean cab=new CourseAboutBean();
			CoursePrerequisiteBean cpb= new CoursePrerequisiteBean();
		
			
			cb.setCourseName(request.getParameter("courseName"));
			cb.setCourseObjective(request.getParameter("objectiveTextarea"));
			Calendar calendar = Calendar.getInstance();
			java.util.Date currentDate = calendar.getTime();
			java.sql.Date date = new java.sql.Date(currentDate.getTime());
			cb.setDate(date);
			cb.setForumURL(request.getParameter("forumURL"));
			cb.setLanguage(request.getParameter("language"));

			int courseID=DatabaseOperations.AddCourse(cb);
			System.out.println("((((((((((((((((((("+courseID);
			if(courseID!=-1){
				System.out.println("Course Succesfully Inserted !");
				System.out.println("***************"+courseID);
				cab.setAbout(request.getParameter("aboutTextarea"));
				cab.setCourseID(courseID);
				boolean isCourseAbout=DatabaseOperations.AddCourseAbout(cab);
				if(isCourseAbout) {
					System.out.println("Course-About inserted successfully");
				}
				else{
					System.out.println("Course-About insertion failed !");
				}

				cpb.setPrerequisite(request.getParameter("prerequisiteTextarea"));
				cpb.setCourseID(courseID);
				boolean isCoursePrereq=DatabaseOperations.AddCoursePrerequisite(cpb);
				if(isCoursePrereq){
					System.out.println("Course-Prerequisite inserted successfully");
				}
				else{
					System.out.println("Course-Prerequisite insertion failed !");
				}
				
				
				HttpSession session = request.getSession(true);
                session.setAttribute("AddedCourseName",cb.getCourseName());
                session.setAttribute("IaddedCourse","yes" );
                response.sendRedirect("profile.jsp");
			}
			else{
				System.out.println("Something Wrongggg in course insertion !");
			    }	
			}
			catch (Throwable exc)
	        {
	            System.out.println(exc);
	        }
			
			
			
			
	
			
			
		
	}

}
